<?php

class M_products extends Model {

    function M_products() {
        parent::Model();
    }
	//fun to select product to show on slide .....by kimsong
	function sl_pro(){
		$this->db->select('*');
        $this->db->from('tbl_page');
		$this->db->ORDER_BY('page_id','DESC');

		return $this->db->get();
	}
	function adver_img(){
		$this->db->select('*');
        $this->db->from('tbl_media');
        $this->db->join('tbl_position', 'tbl_media.media_position_id = tbl_position.pos_id');
		
		return $this->db->get();
	}
    // ====fun add products by kimsong.san ======
	
    function addproduct() {
        $data = array(
            'pro_name' => $this->input->post('pro_name'),
            'pro_price' => $this->input->post('pro_price'),
            'pro_type' => $this->input->post('pro_type'),
            'pro_date' => date('d-m-y'),
            'pro_description' => $this->input->post('pro_desc'),
            'pro_img' => $this->session->userdata('pro_img'),
            'pro_cate_id' => $this->input->post('pro_cate'),
            'pro_user_id' => $this->session->userdata('id'),
            'pro_statu_id' => $this->input->post('pro_statu')
        );

        return $this->db->insert('tbl_product', $data);
    }
   
    // ====End fun add products by rina pov ======
	//=====Start function sub menu by Mr Thary====
	// ====End fun view products by kimsong.san ======
	function com_phon($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('tbl_product');
        $this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
        $this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
        
       // $this->db->where('tbl_product.pro_user_id', $this->session->userdata('id'));
		$this->db->ORDER_BY('pro_id','DESC');
		
        return $this->db->get();
    }
	
	//oducts by Mr Thary ======
	// start view products by rina....
	
    function view_product($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('tbl_product');
        $this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
        $this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
        
        $this->db->where('tbl_product.pro_user_id', $this->session->userdata('id'));
		$this->db->ORDER_BY('pro_id','DESC');

		return $this->db->get();
}
		
        /*
    }*/
	//Search product
	 function search_model($search, $cate) {
        //$this->db->select('Pro_name');
        $this->db->select('*');
        //$this->db->select('Cate_ID');
        $this->db->from('tbl_product');
        $this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
        $this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
        $this->db->like('pro_name', $search); //here can be 'before' or 'after'
        $this->db->where('tbl_category.cate_id', $cate);
        //$this->db->join('category','product.Cate_ID = category.Cate_ID');

        return $this->db->get();
    }

    // ====End fun view products by kimsong.san ======
	function allproduct($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('tbl_product');
        $this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
        $this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
        
       // $this->db->where('tbl_product.pro_user_id', $this->session->userdata('id'));
		$this->db->ORDER_BY('pro_id','DESC');
		
        return $this->db->get();
    }
	//show product detail ....
	function pro_detail(){
		$this->db->select('*');
		$this->db->from('tbl_product');
		$this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
		$this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
		$getpro_id = $this->uri->segment(3);
		
		$this->db->where('tbl_product.pro_id',$getpro_id);
		return $this->db->get();
		
	}
	// ====fun delete products by kimsong.san ======
    function del_pro($data) {
        $this->db->delete('tbl_product', $data);
    }
	
	//=== fun select for update product ===
	function sel_up(){
	   $this->db->select('*');
        $this->db->from('tbl_product');
        $this->db->join('tbl_user', 'tbl_product.pro_user_id = tbl_user.user_id');
        $this->db->join('tbl_category', 'tbl_product.pro_cate_id = tbl_category.cate_id');
		$getId = $this->uri->segment(3);
		
		
		$this->db->where('tbl_product.pro_id', $getId);
		return $this->db->get();
		}
	/*
	*	update product of users login....
	*
	*/
	// ====fun add products by kimsong.san ======
    function m_update_product() {
        $data = array(
            'pro_name' => $this->input->post('pro_name'),
            'pro_price' => $this->input->post('pro_price'),
            'pro_type' => $this->input->post('pro_type'),
            'pro_date' => date('d-m-y'),
            'pro_description' => $this->input->post('pro_desc'),
			
            'pro_cate_id' => $this->input->post('pro_cate'),
            'pro_user_id' => $this->session->userdata('id'),
            'pro_statu_id' => $this->input->post('pro_statu')
        );
		
		$getId = $this->input->post('getpro_id');
		
	    $this->db->where('tbl_product.pro_id', $getId);
	   
       return $this->db->update('tbl_product', $data);
    }
	// ====fun add products by kimsong.san ======
    function m_change_pro() {
        $data = array(
			'pro_img' => $this->session->userdata('pro_img')		  
        );
		
		$getId = $this->input->post('getoldimg');
		
		//$getImg = $this->input->post('getimg');
		$this->session->set_userdata('getImg',$getId);
	    $this->db->where('tbl_product.pro_img', $getId);
       return $this->db->update('tbl_product', $data);
    }
	// change user images ....'pro_img' => $this->session->userdata('pro_img'),
	// $getImg = $this->input->post('getimg');
	// $this->session->set_userdata('getImg',$getImg);
	
	function change_photo(){
		$data = array(
            'user_img' => $this->session->userdata('user_img')
           
        );
		$this->session->set_userdata('getimg',$this->input->post("getoldimg"));
		
	    $getId = $this->session->userdata('id');
	    $this->db->where('tbl_user.user_id', $getId);
	   
       return $this->db->update('tbl_user', $data);	
	}
	function setstatu(){
		$setsta = $this->uri->segment(3);
		$getpro_id = $this->uri->segment(4);
		if($setsta == 1){
			 $data = array(
					'pro_statu_id' => '0' 
					);
			 $this->db->where('pro_id',$getpro_id );
			return $this->db->update('tbl_product',$data);
			
			}else{
				$data = array(
					'pro_statu_id' => '1'
					);
				$this->db->where('pro_id',$getpro_id);
			return $this->db->update('tbl_product',$data);
				}
		}
	// add advertising images here by kimsong.san.....
	function add_adver(){
			$addver = array(
					'media_name' => $this->input->post('adverName'),
					'media_img' => $this->session->userdata('adver_img'),
					'media_url' => $this->input->post('adverUrl'),
					'media_date' => date('d/M/Y'),
					'media_position_id' => $this->input->post('adverPos'),
					'media_statu_id' =>  $this->input->post('adverstatu')
					);
			return $this->db->insert('tbl_media',$addver);
		}
	// ==== fun view all advertises by kimsong.san ======
	function alladver($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('tbl_media');
        $this->db->join('tbl_position', 'tbl_media.media_position_id = tbl_position.pos_id');
        
       // $this->db->where('tbl_product.pro_user_id', $this->session->userdata('id'));
		$this->db->ORDER_BY('media_id','DESC');
		
        return $this->db->get();
    }
	//fun select adver to update...
	function selItem_update(){
		$this->db->select('*');
		$this->db->from('tbl_media');
        $this->db->join('tbl_position', 'tbl_media.media_position_id = tbl_position.pos_id');
		
		$gid = $this->uri->segment(3);
		$this->db->where('tbl_media.media_id',$gid);
		return $this->db->get();
		
	}
	// function selItem_update change data of advertising ..
	function update_adver(){
			$addver = array(
					'media_name' => $this->input->post('adverName'),
					'media_img' => $this->session->userdata('adver_img'),
					'media_url' => $this->input->post('adverUrl'),
					'media_date' => date('d/M/Y'),
					'media_position_id' => $this->input->post('adverPos'),
					'media_statu_id' =>  $this->input->post('adverstatu')
					);
			$getadver_id = $this->input->post('adver_img');
			$this->db->where('tbl_media.media_img',$getadver_id);
			
			$this->session->set_userdata('getadvImg',$getadver_id);
			return $this->db->update('tbl_media',$addver);
		}
	// ====fun delete advertise by kimsong.san ======
	function del_adver($data) {
        $this->db->delete('tbl_media', $data);
    }
	// add advertising images here by kimsong.san.....
	function add_news(){
			$news = array(
					'page_title' => $this->input->post('news_title'),
					'page_text' => $this->input->post('news_text'),
					'page_img' => $this->session->userdata('news_img'),
					'page_date' =>date('d/M/Y'),
					'page_statu' =>  $this->input->post('news_statu')
					);
			return $this->db->insert('tbl_page',$news);
		}
	// ==== fun view all advertises by kimsong.san ======
	function allnews($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('tbl_page');
        
       // $this->db->where('tbl_product.pro_user_id', $this->session->userdata('id'));
		$this->db->ORDER_BY('page_id','DESC');
		
        return $this->db->get();
    }
	// ====fun delete products by kimsong.san ======
    function del_news($data) {
        $this->db->delete('tbl_page', $data);
    }
	function news_detial(){
		$this->db->select('*');
        $this->db->from('tbl_page');
        
       	$this->db->where('tbl_page.page_id', $this->uri->segment(3));
		$this->db->ORDER_BY('page_id','DESC');
		
        return $this->db->get();
	}
   // comment on products
   //User comments
	function comment(){
		$format = 'DATE_RFC850';
		$time = time();	
		//$this->load->database();
		$comment = array(
			'com_desc'=> $this->input->post('text'),
			'com_date'=> standard_date($format, $time),
			'com_user_id' => $this->session->userdata('id'),
			'com_pro_id' => $this->session->userdata('com')
			);
		//die($this->session->userdata('com_getID'));
		return $this->db->insert('tbl_comment', $comment);
    		}
	//select comment
	function show_comment(){
		$this->db->select('*');
		$this->db->from('tbl_comment');
		$this->db->join('tbl_user','tbl_comment.com_user_id = tbl_user.user_id');
		$this->db->join('tbl_product','tbl_comment.com_pro_id = tbl_product.pro_id');
		//$this->db->count_all('tbl_comment');
		$this->db->where('tbl_comment.com_pro_id', $this->uri->segment(3));
		
		$this->db->order_by('com_date','DESC');
		
		return $this->db->get();
	}
	//fun search
	//Search products
	function sear_pro($search){
		$this->db->select('*');
		$this->db->from('tbl_product');
	     $this->db->like('pro_name', $search); //here can be 'before' or 'after'
	     
		return $this->db->get();
		}
}

